package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class AppConfig {

    /**
     * 配置线程池
     */
    @Bean(name = "taskExecutor")
    public ExecutorService taskExecutor() {
        // 创建一个线程池，核心线程数为2，最大线程数为5，
        // 空闲线程存活时间30秒，使用LinkedBlockingQueue作为任务队列
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
            2,  // 核心线程数
            5,  // 最大线程数
            30, // 空闲线程存活时间
            TimeUnit.SECONDS,
            new java.util.concurrent.LinkedBlockingQueue<>(100)  // 任务队列
        );
        
        // 设置拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        
        return executor;
    }
}
    